var S4 = function () {
  return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};

function loadJS(url, callback) {
  var script = document.createElement('script'),
    fn = callback || function () {};
  script.type = 'text/javascript';
  if (script.readyState) {
    script.onreadystatechange = function () {
      if (script.readyState == 'loaded' || script.readyState == 'complete') {
        script.onreadystatechange = null;
        fn();
      }
    };
  } else {
    script.onload = function () {
      fn();
    };
  }
  script.src = url;
  document.getElementsByTagName('head')[0].appendChild(script);
}

var $Util = {
  stringify: Qs.stringify,
  getQueryString: function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
  },
  toggleAttr: function (arr, index, attrName = 'active') {
    arr.forEach(element => {
      element[attrName] = false;
    });
    if (index instanceof Function) {
      index = arr.findIndex(index);
    }
    arr[index] && (arr[index][attrName] = true);
  },
  getRandomRgbColor: function () {
    return new Array(3).fill(255).map((o) => {
      return o * Math.random();
    });
  },
  uuid: function () {
    return S4() + S4() + S4();
  },
  filterUtil: (list, name, type = 'type') => {
    return list.filter((o) => o[type] == name);
  },
  getFmtDate: (date, fmt = 'yyyy-MM-dd') => {
    if (!date) {
      return '';
    }
    if (!(date instanceof Date)) {
      date = new Date(date);
    }
    var o = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds(),
      "q+": Math.floor((date.getMonth() + 3) / 3),
      "S": date.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      }
    }
    return fmt;
  },
  getUsedFmtDate: (date, fmt = 'hh:mm:ss') => {
    return $Util.getFmtDate(date - 28800000, fmt);
  },
  formatDuring: (mss, disableBeauty) => {
    var days = parseInt(mss / (1000 * 60 * 60 * 24));
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = parseInt((mss % (1000 * 60)) / 1000);
    var totalHours = days * 24 + hours;
    if (totalHours > 3 || disableBeauty) { // 取消美化，否则短时间会变成分:秒 
      return totalHours + ":" + minutes + ":" + seconds;
    } else {
      return (totalHours * 60 + minutes) + ":" + seconds;
    }
  },
  engFormatter(number) {
    if (!number) {
      return '0';
    }
    const numbers = number.toString().split('').reverse()
    const segs = []

    while (numbers.length) segs.push(numbers.splice(0, 3).join(''))

    return segs.join(',').split('').reverse().join('')
  }
};

var $Filters = {
  productUrl: function (v) {
    let config = window.$Config || parent.$Config;
    return (config && config.SERVER_PATH || '') + v;
  }
};

const storage = window.sessionStorage;
const JSON = window.JSON;
class DBUtil {
  constructor() {

  }
  get(name) {
    let value = storage.getItem(name);
    if (/^\{.*\}$/.test(value)) value = JSON.parse(value);
    return value;
  }
  set(name, value) {
    if (typeof value === typeof {}) value = JSON.stringify(value);
    return storage.setItem(name, value);
  }
  getObject(name) {
    return JSON.parse(storage.getItem(name));
  }
  remove(name) {
    return storage.removeItem(name);
  }
}
var $db = new DBUtil();
